<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>splice 方法示例</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="app">
        <h2>splice 方法示例</h2>
        
        <h3>原始数组:</h3>
        <ul>
            <li v-for="(item, index) in items" :key="index">{{ item }}</li>
        </ul>

        <h3>操作示例:</h3>
        <button @click="removeFirst">删除第一个元素</button>
        <button @click="addNewItem">在索引1处插入新元素</button>
        <button @click="replaceElement">替换索引2的元素</button>
        <button @click="resetArray">重置数组</button>

        <h3>操作后的数组:</h3>
        <ul>
            <li v-for="(item, index) in items" :key="index">{{ item }}</li>
        </ul>
    </div>

    <script>
        const { createApp } = Vue;
        
        createApp({
            data() {
                return {
                    items: ['苹果', '香蕉', '橙子', '葡萄', '草莓']
                };
            },
            methods: {
                removeFirst() {
                    // 删除第一个元素
                    this.items.splice(0, 1);
                },
                addNewItem() {
                    // 在索引1处插入新元素
                    this.items.splice(1, 0, '芒果');
                },
                replaceElement() {
                    // 替换索引2的元素
                    this.items.splice(2, 1, '柠檬');
                },
                resetArray() {
                    // 重置数组
                    this.items = ['苹果', '香蕉', '橙子', '葡萄', '草莓'];
                }
            }
        }).mount('#app');
    </script>
</body>
</html>